import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
import { PositionHelper } from "utils/position/PositionHelper";
import { UtilsHelper } from "utils/position/UtilsHelper";
 

/**
侦察爬。改成随机走吧
 */ 
export class ScoutTask<T extends IStateArg<Creep>>  extends State<T>
{

    //
    public  getActions(arg:T):Action[]
    {
        
        const memory = arg.entity.getNodeMemory();
        if(!memory.taskid)
        {
            return [];
        }
        const task =arg.task as ITask<ICreepTaskData<"scout">>;
        if(!task)
        {
            // 查找不到任务
            return []
        }
        const creep = arg.entity;
    
        if(task.data.arg.timecost)
        {
            task.data.arg.timecost--;
        }
        else 
        {
            task.data.arg.timecost = 1400;
            task.data.arg.targets.splice(0,1);
        }
        const idx = task.data.arg.targets.findIndex(e=>e==creep.room.name);
        

        if(((idx>-1 || !PositionHelper.isNabRoom( task.data.arg.targets[0],creep.room.name) )&&  !PositionHelper.isInExit(creep.pos))||task.data.arg.targets.length ==0)
        {
            // 到了新房间,更新一下下一次要走的房间
            // Game.map.findExit()
           
            const nextNewRooms = Object.values(Game.map.describeExits(creep.pos.roomName)).filter(
                e=>  Object.values(UtilsHelper.RoomPos(e)).every(v=>v<=60)  );
            // 如果下一个房间全都是访问过的则。对访问列表进行清空
            const notVisited = nextNewRooms.filter(e=>!task.data.arg.visits.includes(e) && !Game.rooms[e]);
            if(notVisited.length==0)
            {
                task.data.arg.visits=[];
                // 访问记录重置
                const nextroom = nextNewRooms[Game.time%nextNewRooms.length];
                task.data.arg.targets = [nextroom]
            }
            else 
            {
                // 更新下一个要走的房间
                if(!task.data.arg.visits.includes(creep.room.name))
                {
                    task.data.arg.visits.push(creep.room.name);
                }
                const nextroom = notVisited[Game.time%notVisited.length];
                task.data.arg.targets = [nextroom]
            }
            //const visit = task.data.arg.targets.splice(idx,1);
            if(task.data.arg.visits.length>20)
            {
                task.data.arg.visits.splice(0,20);
            }
            task.data.arg.timecost = 1400;// 每次查找到新房间就刷新一次;
            // global.Log.Info(`删除侦察到的房间之后 ${task.data.arg.targets}`);
            // task.data.arg.visits.push(...visit);
            // // 因为新的出口一定是当前房间最近的目标。所以不需要排序
            // task.data.arg.targets = nextNewRooms.concat(task.data.arg.targets);
            // task.data.arg.targets = _.sortBy(nextNewRooms.concat(task.data.arg.targets),e=>Game.map.getRoomLinearDistance(e,creep.room.name,false));
        }

       
        if(task.data.arg.targets.length>0)
        {
            const targetroom = task.data.arg.targets[0];
            const pos = PositionHelper.Create({roomName:targetroom,x:25,y:25});
            // if(!creep.memory._move?.path && PositionHelper.isNabRoom( creep.pos.roomName,targetroom) )
            // {
            //     // && !PositionHelper.isInExit(creep.pos)
            //     {
            //         creep.log("warn",`尝试检测是否碰到了新手墙!`,creep.room.name,`已访问:${task.data.arg.visits.length}`);
            //         if(creep.memory._move.goal?.code ==1)
            //         {
            //             const rooms = task.data.arg.targets.splice(0,1);
            //             creep.log("warn",`撞到了新手墙!`,JSON.stringify(creep.memory._moveCache.goal),`删除房间`, rooms);
            //             memory._moveCache.goal={pos: pos,range:10};
            //             if(task.data.arg.visits.find(e=>e ==rooms[0]))
            //             {
            //                 return[];
            //             }
            //             task.data.arg.visits.push(...rooms);
                      
            //             return []
            //         }
            //     }
            // }
            // creep.log("info",`移动目标`);
            
            return [ActionArgHelper.actionMoveto(arg.entity,{pos: pos,range:10},{})]
        }
        else 
        {
            creep.log("error",`盏茶任务没有目标`);
        }
        return []
    }
}
